﻿<template>
    <div class="GroupInfoDetail">
        <MyDialog :model-value="true" :type="DialogType.C1" :title="title" @close="onClose">
            <el-form ref="GroupInfoDetailFormRef" label-width="110" :model="thisData" :rules="rules">
                <el-form-item label="编码" prop="code">
                    <el-input v-model="thisData.code" maxlength="50" :disabled="!Gx.IsEmpty(rowAutoID)"></el-input>
                </el-form-item>

                <el-form-item label="名称" prop="name">
                    <el-input v-model="thisData.name" maxlength="200" :disabled="!Gx.IsEmpty(rowAutoID)"></el-input>
                </el-form-item>

                <el-form-item label="管理员用户名" prop="loginName">
                    <el-input v-model="thisData.loginName" maxlength="50" :disabled="rowAutoID!=null"></el-input>
                </el-form-item>

                <el-form-item label="管理员密码" prop="loginPassword" v-if="rowAutoID == null">
                    <el-input v-model="thisData.loginPassword" maxlength="200"></el-input>
                </el-form-item>

                <el-form-item label="负责人" prop="leaderName">
                    <el-input v-model="thisData.leaderName" maxlength="50"></el-input>
                </el-form-item>

                <el-form-item label="电话" prop="leaderPhone">
                    <el-input v-model="thisData.leaderPhone" maxlength="20"></el-input>
                </el-form-item>

                <el-form-item label="备注" prop="remark">
                    <el-input v-model="thisData.remark" maxlength="500"></el-input>
                </el-form-item>
            </el-form>

            <template #footer>
                <el-button type="primary" @click="onSave" :loading="loading">保存</el-button>
            </template>
        </MyDialog>
    </div>
</template>

<script lang="ts" setup>
import Enums from "@/gx/Enums";
import Gx from "@/gx/Gx";
import { FormRules } from "element-plus";
import { DialogType } from "@/_gx/TsEnums";

//接收父组件的传值
const rowAutoID = inject("rowAutoID") as Ref<number | null>;

//定义当前页面数据对象
const thisData = ref<any>({});

const loading = ref(false);

//定义表单验证规则
const rules = ref<FormRules>({
    code: [{ required: true, message: "请输入编码", trigger: "blur" }],
    name: [{ required: true, message: "请输入名称", trigger: "blur" }],
    loginName: [{ required: true, message: "请输入管理员用户名", trigger: "blur" }],
    loginPassword: [{ required: true, message: "请输入管理员密码", trigger: "blur" }],
});

//标题
const title = computed(() => {
    if (Gx.IsEmpty(rowAutoID.value)) {
        return "集团详情 - 创建";
    } else {
        return `集团详情 - 编辑 - ${thisData.value.name}`;
    }
});

//初始化
const Init = () => {
    if (!Gx.IsEmpty(rowAutoID.value)) {
        App.CallApi(Api.GroupInfo.GetByID(rowAutoID.value), {
            success: (res) => {
                thisData.value = res;
            },
        });
    }
};

//表单Ref对象
const GroupInfoDetailFormRef = ref(null);

//保存
const onSave = () => {
    GroupInfoDetailFormRef.value.validate((isOk: boolean) => {
        if (!isOk) {
            return;
        }

        let _api = null;
        let _data = thisData.value;
        if (Gx.IsEmpty(rowAutoID.value)) {
            //添加
            _api = Api.GroupInfo.Insert;
        } else {
            //修改
            _api = Api.GroupInfo.Update;
        }

        loading.value = true;
        App.CallApi(_api, {
            data: _data,
            success: (res) => {
                emits("close", true);
            },
            complete: () => {
                loading.value = false;
            },
        });
    });
};

//关闭
const onClose = () => {
    emits("close", false);
};

//向上抛出的事件与方法
const emits = defineEmits(["close"]);
defineExpose({ Init });
</script>
